package com.example.demo.controller;

import com.example.demo.Interface.UserRepo;
import com.example.demo.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class Login {
    @Resource
    UserRepo userRepo;
    @RequestMapping(value = "/")
    public String index(){
        return "index";
    }

    /*
    * 登录
    *
    * */
    @RequestMapping(value = "/login")
    @ResponseBody
    public String login(HttpServletRequest req, HttpServletResponse rsp) {
        String name = req.getParameter("name");
        String password = req.getParameter("password");

        //System.out.print(name);

        List<User> optionalUser = userRepo.findAll();
        for (User user : optionalUser) {
          String name1 = user.name;
          if(name.equals(name1)){
              if(password.equals(user.password)){
                  return "losuccess";//登录成功
              }
          }
        }
        return "failure";//登录失败

    }
}
